package com.ssy.lingxi.member.merchant.entity;

import com.fasterxml.jackson.annotation.JsonBackReference;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonManagedReference;

import javax.persistence.*;
import java.io.Serializable;
import java.util.Objects;
import java.util.Set;

/**
 * 业务平台角色 - 数据来源于Pass平台的配置
 * @author 万宁
 * @version 2.0.0
 * @date 2020-06-13
 */
@JsonIgnoreProperties(value={"hibernateLazyInitializer","handler","fieldHandler"})
@Entity
@Table(schema = "public", name = "ms_mc_role", indexes = {@Index(name = "ms_mc_role_role_name_idx", columnList = "roleName"), @Index(name = "ms_mc_role_role_type_id_idx", columnList = "role_type_id"), @Index(name = "ms_mc_role_member_type_id_idx", columnList = "member_type_id"), @Index(name = "ms_mc_role_rel_type_idx", columnList = "relType")})
public class RoleDO implements Serializable {
    private static final long serialVersionUID = 1092592933626952363L;

    @Id
    @GeneratedValue(strategy= GenerationType.SEQUENCE,generator="role_seq")
    @SequenceGenerator(sequenceName="ms_mc_role_seq", name="role_seq",allocationSize = 1)
    private Long id;

    /**
     * 角色归属类型，定义在 MemberRelationTypeEnum 中，0-平台后台角色，1-普通会员角色
     */
    @Column
    private Integer relType;

    /**
     * 角色名称
     */
    @Column(columnDefinition = "varchar(50)", nullable = false)
    private String roleName;

    /**
     * 状态（1=有效,0=无效）
     */
    @Column(columnDefinition = "int2", nullable = false)
    private Integer status;

    /**
     * 多对一单向关联业务类型
     */
    @ManyToOne(cascade = CascadeType.DETACH, fetch = FetchType.LAZY)
    @JoinColumn(name = "business_type_id")
    private MemberBusinessTypeDO businessType;

    /**
     * 多对一单向关联角色类型
     */
    @ManyToOne(cascade = CascadeType.DETACH, fetch = FetchType.LAZY)
    @JoinColumn(name = "role_type_id")
    private RoleTypeDO roleType;

    /**
     * 多对一单向关联会员类型
     */
    @ManyToOne(cascade = CascadeType.DETACH, fetch = FetchType.LAZY)
    @JoinColumn(name = "member_type_id")
    private MemberTypeDO memberType;

    /**
     * 一对一双向关联权限配置
     */
    @JsonManagedReference
    @OneToOne(cascade = {CascadeType.PERSIST, CascadeType.REMOVE}, fetch = FetchType.LAZY)
    @JoinColumn(name="role_auth_id",referencedColumnName="id")
    private RoleAuthDO roleAuth;

    /**
     * 多对多双向关联会员
     */
    @JsonBackReference
    @ManyToMany(mappedBy = "roles", cascade=CascadeType.DETACH, fetch=FetchType.LAZY)
    private Set<MemberDO> members;

    /**
     * 多对多双向关联会员注册资料
     */
    @JsonManagedReference
    @ManyToMany(cascade = CascadeType.DETACH, fetch = FetchType.LAZY)
    @JoinTable(name = "ms_mc_role_config_relation", joinColumns={ @JoinColumn(name="role_id", referencedColumnName="id")}, inverseJoinColumns={@JoinColumn(name="member_config_id", referencedColumnName="id")})
    private Set<MemberConfigDO> configs;

    public RoleDO() {
    }

    public RoleDO(Long id) {
        this.id = id;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public Integer getRelType() {
        return relType;
    }

    public void setRelType(Integer relType) {
        this.relType = relType;
    }

    public String getRoleName() {
        return roleName;
    }

    public void setRoleName(String roleName) {
        this.roleName = roleName;
    }

    public Integer getStatus() {
        return status;
    }

    public void setStatus(Integer status) {
        this.status = status;
    }

    public MemberBusinessTypeDO getBusinessType() {
        return businessType;
    }

    public void setBusinessType(MemberBusinessTypeDO businessType) {
        this.businessType = businessType;
    }

    public RoleTypeDO getRoleType() {
        return roleType;
    }

    public void setRoleType(RoleTypeDO roleType) {
        this.roleType = roleType;
    }

    public MemberTypeDO getMemberType() {
        return memberType;
    }

    public void setMemberType(MemberTypeDO memberType) {
        this.memberType = memberType;
    }

    public RoleAuthDO getRoleAuth() {
        return roleAuth;
    }

    public void setRoleAuth(RoleAuthDO roleAuth) {
        this.roleAuth = roleAuth;
    }

    public Set<MemberDO> getMembers() {
        return members;
    }

    public void setMembers(Set<MemberDO> members) {
        this.members = members;
    }

    public Set<MemberConfigDO> getConfigs() {
        return configs;
    }

    public void setConfigs(Set<MemberConfigDO> configs) {
        this.configs = configs;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (o == null || getClass() != o.getClass()) {
            return false;
        }
        RoleDO roleDO = (RoleDO) o;
        return Objects.equals(id, roleDO.id);
    }

    @Override
    public int hashCode() {
        return Objects.hash(id);
    }
}
